/*
 * Copyright (C) 2012 Martin Wilsdorf <mwjutils-api@googlecode.com>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

package com.google.mwjutils.xcode.samples;

import com.google.mwjutils.xcode.XShell;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.FileSystems;

/**
 *
 * @author Martin Wilsdorf <mwjutils-api@googlecode.com>
 */
public final class MWXShell extends XShell {
    public static void main( String ... aArgs ) {
        MWXShell.main( new MWXShell( ), aArgs);
    }
    
    private static String processPromptPath( String path ) {
        path = FileSystems.getDefault( ).getPath( path.trim() )
                .normalize( ).toAbsolutePath( ).toString( );
        return path;
    }
    
    public MWXShell( ) {
        super( );
        setDefaultVars();
        setVar( "PROMPT", _DEF_PROMPT );
    }
    
    public String cd( String ... aArgs ) {
        String path = new String( );
        if ( aArgs.length == 0 ) {
            path = fPrevPath;
        } else {
            for ( int i = 0; i < aArgs.length;
                    path = path.concat( aArgs[i++] ) + " " ) { }
            if ( FileSystems.getDefault( ).getPath( path.trim( ) )
                    .isAbsolute( ) || path.startsWith( _SLASH ) ) {
                path = processPromptPath( path );
            } else {
                path = processPromptPath( fPath + _SLASH + path.trim() );
            }
        }
        if ( path.equals( fPath ) ) {
            echo( "pwd not changed." );
        } else {
            if ( new java.io.File( path ).exists( ) ) {
                fPrevPath = fPath;
                fPath = path;
            } else {
                error( new Exception( "directory not found" ), "cmd", "cd", false );
            }
        }
        return fPath;
    }
    
    @Override
    public String echo( String ... aArgs ) {
        return super.echo( aArgs );
    }
    
    @Override
    public void env( String ... aArgs ) {
        super.env( aArgs );
    }
    
    public String getHost( ) {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ex) {
            return "localhost";
        }
    }

    public String getPwd( ) {
        return fPath;
    }

    public String getUser( ) {
        return System.getProperty("user.name");
    }
    
    @Override
    public String getVar( String aKey ) {
        setDefaultVars();
        return super.getVar(aKey);
    }
    
    public void help( String ... aArgs ) {
        echof( _DEF_MSG_HELP );
    }
    
    @Override
    public String[] list( String ... aArgs ) {
        return super.list( aArgs );
    }
    
    public void ls( String ... aArgs ) {
        for( String file:new java.io.File(fPath).list( ) ) {
            echo( file );
        }        
    }
    
    public void pwd( String ... aArgs ) {
        echo( getPwd() );
    }
    
    @Override
    public String run() {
        echof( _DEF_MSG_WELCOME );
        String result = super.run();
        echof( _DEF_MSG_EXIT );
        return result;
    }
    
    public void setDefaultVars( ) {
        setVar( "USER", getUser( ) );
        setVar( "HOST", getHost( ) );
        setVar( "PWD", getPwd( ) );
    }

    public final static String _DEF_MSG_HELP =
"This is a sample of XShell that uses XLineReader for interpreting every%n" +
"commited line to the standard input (console usually).%n%n" +
"Type 'list' for a list of available commands.%n";
    public final static String _DEF_MSG_WELCOME = _DEF_MSG_HELP;
    public final static String _DEF_MSG_EXIT = "Good job, have a nice day!";
    public final static String _DEF_PATH = ".";
    public final static String _DEF_PROMPT = "%n<USER>@<HOST> <PWD>%n$ ";
    public final static String _SLASH = FileSystems.getDefault().getSeparator();
    private String  fPath     = processPromptPath(_DEF_PATH);
    private String  fPrevPath = fPath;
}
